<?php
namespace app\api\controller;

class Course
{
   //农产品产品接口
   public function index($offset,$limit) {
       $courseRes = model('Course')->field('*')->where('status',1)->limit($offset,$limit)->select();
       $courseUnm = model('Course')->field('*')->where('status',1)->select();
       $courseUnm = count($courseUnm);
       $data = ['data'=>$courseRes,'sum'=>$courseUnm, 'status'=> 0];
       return json($data);
   }
   //按分类的产品的接口
   public function CategoryPages($id) {//SELECT catid,pid FROM jy_category WHERE pid =".$sort_id;
       $courseRes = model('Course')->field('*')->where('cate_id',$id)->select();
       $coursenum = count($courseRes);
       $data = ['data'=>$courseRes, 'num'=>$coursenum, 'status'=> 0];
       return json($data);
   }
   //畅销好课接口
   public function soldGood() {
        $courseRes = array();
        $_courseRes = model('Course')->field('id, cor_name, cor_pic')->order('sold_num desc')->limit(6)->select()->toArray();
        $courseRes = sliceArray($_courseRes,2,3);
        $data = ['data'=>$courseRes, 'status'=> 0];
        return json($data);
    }
    //首页数据
    public function indexDataBlock() {
        $block = array();
        $cateRes = db('cate')->where(['show_index'=>1])->select();
        foreach($cateRes as $k => $v) {
            $reCourse = model('Course')->field('id, cor_name,cor_pic,cor_price')->where(['recommend' => 1,'cate_id' => $v['id']])->find()->toArray();
            $courseRes = array();
            $_courseRes = model('Course')->field('id, cor_name, cor_pic')->where(['recommend' => 0,'cate_id' => $v['id']])->order('sold_num desc')->limit(6)->select()->toArray();
            $courseRes = sliceArray($_courseRes,2,2);
            $block[$k]['cate_name'] = $v['cate_name'];
            $block[$k]['reCourse'] = $reCourse;
            $block[$k]['CourseRes'] = $courseRes;
        }
        $data = ['data'=>$block, 'status'=> 0]; 
        return json($data);
    }
    //产品详情接口
    public function courseDetail($id) {
        $courseDes =model('Course')->where('id',$id)->select();
        // 课时
        $data = ['data'=>$courseDes, 'status'=> 0];
        return json($data);    
    }
    //搜索功能接口
    public function search($search) {
       $courseRes = model('Course')->where('cor_name','like','%'.$search.'%')->order(['id'=>'desc'])->select();
       return $courseRes;
    }
}
